package com.itcat.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.UmsAdminRoleApi;
import com.itcat.mapper.UmsAdminRoleMapper;
import com.itcat.mysql.pojo.UmsAdmin;
import com.itcat.mysql.pojo.UmsRole;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/3/31 19:32]
 */
@DubboService
public class UmsAdminRoleApiImpl implements UmsAdminRoleApi {

    @Autowired
    private UmsAdminRoleMapper mapper;

    @Override
    public List<UmsRole> getUmsRole(Long umsAdmin) {
        return mapper.getUmsRole(umsAdmin);
    }

    @Override
    public List<UmsRole> selectUmsRole(Long umsAdmin) {
        return mapper.selectRole(umsAdmin);
    }

    @Override
    public List<UmsRole> listAll() {
        return mapper.selectList(new LambdaQueryWrapper<UmsRole>().eq(UmsRole::getStatus, 1L));
    }

    @Override
    public Page<UmsRole> selectPage(String name, Integer pageNum, Integer pageSize) {
        Page<UmsRole> rolePage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsRole> lqw = new LambdaQueryWrapper<>();
        lqw.like(!StringUtils.isEmpty(name), UmsRole::getName, name);
        Page<UmsRole> page = mapper.selectPage(rolePage, lqw);
        return page;
    }

    /***
     * 添加角色
     * @author Cr Mr
     * @date 2022/4/3 10:59
     */
    @Override
    public void create(UmsRole umsRole) {
        mapper.insert(umsRole);
    }

    /***
     * 给角色分配菜单
     * @author Cr Mr
     * @date 2022/4/3 11:04
     */
    @Override
    public void allocMenu(Long roleId, List<Long> menuIds) {
        List<Long> list = new ArrayList<>();
        list.add(roleId);
        mapper.deleteMenu(list);
        mapper.allocMenu(roleId, menuIds);
    }

    /***
     * 批量删除角色
     * @author Cr Mr
     * @date 2022/4/3 11:27
     */
    @Override
    public void delete(List<Long> ids) {
        mapper.deleteBatchIds(ids);
        mapper.deleteMenu(ids);
        mapper.deleteResource(ids);
    }

    /***
     * 修改角色状态
     * @author Cr Mr
     * @date 2022/4/3 13:14
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        LambdaUpdateWrapper<UmsRole> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UmsRole::getId, id).set(UmsRole::getStatus, status);
        mapper.update(null, wrapper);
    }

    /***
     * 修改角色
     * @author Cr Mr
     * @date 2022/4/3 13:14
     */
    @Override
    public void update(Long id, UmsRole umsRole) {
        mapper.update(umsRole, new LambdaUpdateWrapper<UmsRole>().eq(UmsRole::getId, id));
    }
}
